import gulp from 'gulp';
import util from 'gulp-util';
import eslint from 'gulp-eslint';
import plumber from 'gulp-plumber';
import notifier from 'node-notifier';
import webpack from 'webpack';
import devConfig from './config/webpack.dev';
import proConfig from './config/webpack.pro';
// import express from 'express';
import webpackDevServer from 'webpack-dev-server';
import path from 'path';
import run from 'run-sequence';

gulp.task('client', () => {
  const compiler = webpack(devConfig);

  compiler.plugin('done', stats => {
    run('lint');
  });

  new webpackDevServer( compiler, {
    contentBase: './',
    publicPath: devConfig.output.publicPath,
    hot: true,
    quiet: false,
    historyApiFallback: true,
    noInfo: false,
    inline: true,
    stats: {
      colors: true,
      chunks: false
    }
  }).listen(3000, '0.0.0.0', (err, stats)=>{
    if (err) util.log(err);
    util.log(`webpack was listenning: http://0.0.0.0:3000`);
  });
});

gulp.task('build', () => {
  const compiler = webpack(proConfig, err => {
    if(!err){
      console.log('build success!!');
    }else{
      // console.err(err);
    }
  });
});

gulp.task('lint', () => {
  gulp.src(['./app/**/*.js', './*.js', './app/**/*.vue', '!./app/**/vendor/**/*'])
    .pipe(eslint())
    .pipe(eslint.results(results => {
      
     // notifier.notify({
     //    title: 'ESLint Error',
     //    wait: true,
     //    message: `Line :`
     //  });
    }))
    .pipe(plumber({
      errorHandler(err){
        const { fileName, lineNumber, message } = err;
        const relativeFilename = path.relative(process.cwd(), fileName);
        notifier.notify({
          title: 'ESLint 错误',
          wait: true,
          message: `第 ${lineNumber} 行(${relativeFilename}):\n\n ${message}`
        });
      }
    }))
    .pipe(eslint.failOnError())
    .pipe(eslint.formatEach());
});